package com.taomu.app.zongchouhuli.utils.dialog;


import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.utils.adpter.OrderDialogBalanceExplainAdp;
import com.taomu.app.zongchouhuli.utils.adpter.OrderDialogBalanceTypeAdp;
import com.taomu.app.zongchouhuli.utils.adpter.OrderDialogBuyNumAdp;
import com.taomu.app.zongchouhuli.utils.entity.OrderExplainEntity;
import com.taomu.app.zongchouhuli.utils.entity.OrderNumEntity;
import com.taomu.app.zongchouhuli.weight.AmountView;

import java.util.List;

public class OrderDialog extends DialogFragment {

    private ImageView mCloseIm;

    private ImageView mIconIm;

    private TextView mProductNameTv;

    private TextView mProductPriceTv;

    private RecyclerView mBalanceTypeRecy;

    private OrderDialogBalanceTypeAdp mTypeAdp;

    private RecyclerView mBalanceExplainRecy;

    private OrderDialogBalanceExplainAdp mExplainAdp;

    private RecyclerView mNumRecy;

    private OrderDialogBuyNumAdp mNumAdp;

    private TextView mLimitNumTv;

    private TextView mSubmitTv;

    private AmountView mAmountView;

    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.dialog_order_view, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        this.setStyle(STYLE_NORMAL, R.style.bottom_up_dialog);
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DpUtils.screnWidth();
        lp.height = DpUtils.dp2px(84+36+48+36*mExplainEntities.size()+36+48+48+72+2);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setAttributes(lp);
        return dialog;
    }

    private void initView(){
        mAmountView = view.findViewById(R.id.amount_view);
        mLimitNumTv = view.findViewById(R.id.dialog_order_buy_limit_tv);
        mCloseIm = view.findViewById(R.id.dialog_order_close_im);
        mIconIm = view.findViewById(R.id.dialog_order_icon_im);
        mProductNameTv = view.findViewById(R.id.dialog_order_name_tv);
        mProductPriceTv = view.findViewById(R.id.dialog_order_price_tv);
        mTypeAdp = new OrderDialogBalanceTypeAdp(getActivity());
        mBalanceTypeRecy = view.findViewById(R.id.dialog_order_balance_type_recy);
        LinearLayoutManager mTypeManager = new LinearLayoutManager(getActivity());
        mTypeManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mBalanceTypeRecy.setLayoutManager(mTypeManager);
        mBalanceTypeRecy.setAdapter(mTypeAdp);
        mExplainAdp = new OrderDialogBalanceExplainAdp(getActivity());
        mBalanceExplainRecy = view.findViewById(R.id.dialog_order_balance_explain_recy);
        mBalanceExplainRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBalanceExplainRecy.setAdapter(mExplainAdp);
        mNumAdp = new OrderDialogBuyNumAdp(getActivity());
        mNumRecy = view.findViewById(R.id.dialog_product_num_recy);
        LinearLayoutManager mNumManager = new LinearLayoutManager(getActivity());
        mNumManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mNumRecy.setLayoutManager(mNumManager);
        mNumRecy.setAdapter(mNumAdp);
        mSubmitTv = view.findViewById(R.id.dialog_order_buy_tv);
        Glide.with(getActivity()).load(pic).into(mIconIm);
    }

    private void initData(){
        mTypeAdp.setData(mBanlanceTypeEntities,mSelectBanlanceTypeEntity);
        mExplainAdp.setData(mExplainEntities,mSelectExplainEntity);
        mNumAdp.setData(mNumEntities,mSelectNumEntity);
        mProductPriceTv.setText(price);
        if(!TextUtils.isEmpty(limitNum)){
            mAmountView.setGoods_storage(Integer.valueOf(limitNum));
            mLimitNumTv.setText("（每日购买最多"+limitNum+"）");
        }
        mNumAdp.setOrderDialogBuyNumListener(new OrderDialogBuyNumAdp.OrderDialogBuyNumListener() {
            @Override
            public void onItemClick(OrderNumEntity entity) {
                if(Integer.valueOf(entity.getOrderNum())<=Integer.valueOf(limitNum)) {
                    mAmountView.setGoods(entity.getOrderNum());
                }
            }
        });
    }

    private void initEvent(){
        mSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    OrderNumEntity entity = new OrderNumEntity();
                    entity.setOrderNum(mAmountView.getAmount());
                    listener.onConfirmClick(mTypeAdp.getSelectData().get(0),mExplainAdp.getSelectData().get(0),entity);
                }
                dismiss();
            }
        });
        mTypeAdp.setOrderDialogBalanceTypeListener(new OrderDialogBalanceTypeAdp.OrderDialogBalanceTypeListener() {
            @Override
            public void onItemClick(OrderExplainEntity entity) {
                mExplainAdp.selectCheck(entity);
            }
        });
        mCloseIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if(listener != null){
            OrderNumEntity entity = new OrderNumEntity();
            entity.setOrderNum(mAmountView.getAmount());
            listener.setSelectBalanceInfo(mTypeAdp.getSelectData().get(0),mExplainAdp.getSelectData().get(0),entity);
        }
        super.onDismiss(dialog);
    }

    private OrderDialogListener listener;

    public void setOrderDialogListener(OrderDialogListener listener){
        this.listener = listener;
    }

    public interface OrderDialogListener{

        void onConfirmClick(OrderExplainEntity mType, OrderExplainEntity mExplain,OrderNumEntity mNumEntity);

        void setSelectBalanceInfo(OrderExplainEntity mType, OrderExplainEntity mExplain,OrderNumEntity mNumEntity);
    }

    private List<OrderExplainEntity> mExplainEntities;

    private OrderExplainEntity mSelectExplainEntity;

    private List<OrderExplainEntity> mBanlanceTypeEntities;

    private OrderExplainEntity mSelectBanlanceTypeEntity;

    private List<OrderNumEntity> mNumEntities;

    private OrderNumEntity mSelectNumEntity;

    private String limitNum;

    public void setBaseData(List<OrderExplainEntity> mBanlanceTypeEntities,OrderExplainEntity mSelectBanlanceTypeEntity,List<OrderExplainEntity> mExplainEntities,OrderExplainEntity mSelectExplainEntity,List<OrderNumEntity> mNumEntities,OrderNumEntity mSelectNumEntity){
        this.mBanlanceTypeEntities = mBanlanceTypeEntities;
        this.mSelectBanlanceTypeEntity = mSelectBanlanceTypeEntity;
        this.mExplainEntities = mExplainEntities;
        this.mSelectExplainEntity = mSelectExplainEntity;
        this.mNumEntities = mNumEntities;
        this.mSelectNumEntity = mSelectNumEntity;
        if(mNumAdp != null){
            mNumAdp.setData(this.mNumEntities,this.mSelectNumEntity);
        }
        if(mExplainAdp != null){
            mExplainAdp.setData(this.mExplainEntities, mSelectExplainEntity);
        }
        if(mTypeAdp != null){
            mTypeAdp.setData(this.mBanlanceTypeEntities,this.mSelectBanlanceTypeEntity);
        }
    }

    public void setLimitNum(String limitNum){
        this.limitNum = limitNum;
    }

    private String price;

    public void setPrice(String price){
        this.price = price;
    }

    private String pic;

    public void setImage(String pic){
        this.pic = pic;
    }
}
